package com.hq.zb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hq.zb.dto.CodeRulesHeaderDto;
import com.hq.zb.model.CodeRulesHeader;
import com.hq.zb.model.CodeRulesLine;
import com.hq.zb.service.ICodeRulesHeaderService;
import com.hq.zb.service.ICodeRulesLineService;
import com.hq.zb.utils.Result;
import com.hq.zb.vo.CodeRulesHeaderVo;
import com.hq.zb.vo.CodeRulesLineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流水号规则Controller.
 *
 * @author huanghanrui
 * @since 2022-03-08
 */
@RestController
@RequestMapping("/sys")
public class CodeRulesHeaderController {

    @Autowired
    private ICodeRulesHeaderService codeRulesHeaderService;

    @Autowired
    private ICodeRulesLineService codeRulesLineService;

    /**
     * 保存流水号规则配置
     * @param codeRulesHeaderDto
     * @return
     */
    @PostMapping("/codeRulesHeader/save")
    public Result saveCodeRule(@RequestBody CodeRulesHeaderDto codeRulesHeaderDto) {
        codeRulesHeaderService.saveCodeRule(codeRulesHeaderDto);
        return Result.ok();
    }

    /**
     * 查询流水号配置
     * @param codeRulesHeaderDto 用户
     * @param pageNum 页
     * @param pageSize 页大小
     * @return
     */
    @PostMapping("/codeRulesHeader/query")
    @ResponseBody
    public Result queryCodeRules(@RequestBody CodeRulesHeaderDto codeRulesHeaderDto, int pageNum, int pageSize) {
        Wrapper<CodeRulesHeaderVo> wrapper = new EntityWrapper<>();
        wrapper.eq("delete_flag", false);
        wrapper.like("rule_code", codeRulesHeaderDto.getRuleCode());
        wrapper.like("rule_name", codeRulesHeaderDto.getRuleName());
        Page<CodeRulesHeaderVo> page = new Page<>(pageNum, pageSize);
        Page<CodeRulesHeaderVo> list = codeRulesHeaderService.queryCodeRules(page, wrapper);
        Result.ok();
        return Result.data(list);
    }

    /**
     * 获取流水号
     * @param ruleCode
     * @return
     */
    @PostMapping("/codeRulesHeader/getCode")
    @ResponseBody
    public String getCode(String ruleCode) {
        return codeRulesHeaderService.getCode(ruleCode);
    }

    /**
     * 查询流水号规则行表
     * @param codeRulesHeaderDto 流水号规则行表
     * @return
     */
    @PostMapping("/codeRulesLine/query")
    @ResponseBody
    public Result queryCodeRulesLine(@RequestBody CodeRulesHeaderDto codeRulesHeaderDto) {
        Wrapper<CodeRulesLineVo> wrapper = new EntityWrapper<>();
        wrapper.eq("delete_flag", false);
        wrapper.eq("rule_code", codeRulesHeaderDto.getRuleCode());
        List<CodeRulesLineVo> codeRulesLines = codeRulesLineService.queryCodeRulesLine(wrapper);
        return Result.data(codeRulesLines);
    }

    /**
     * 删除流水号规则
     * @param codeRulesHeaders 数据字典集合
     * @return
     */
    @PostMapping("/codeRulesHeader/delete")
    @ResponseBody
    public Result deleteCodes(@RequestBody List<CodeRulesHeader> codeRulesHeaders) {
        codeRulesHeaderService.deleteCodeRulesHeader(codeRulesHeaders);
        return Result.ok();
    }

    /**
     * 删除流水号规则行
     * @param codeRulesLines
     * @return
     */
    @PostMapping("/codeRulesLine/delete")
    @ResponseBody
    public Result deletecodeRulesLine(@RequestBody List<CodeRulesLine> codeRulesLines) {
        codeRulesHeaderService.deletecodeRulesLine(codeRulesLines);
        return Result.ok();
    }
}

